

package com.loong.loong.hik.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loong.loong.common.core.util.R;
import com.loong.loong.common.log.annotation.SysLog;
import com.loong.loong.hik.entity.WatchLine;
import com.loong.loong.hik.service.WatchLineService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.loong.loong.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 巡更
 *
 * @author jinyiang
 * @date 2023-03-30 14:19:00
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/watchline" )
@Api(value = "watchline", tags = "巡更管理")
public class WatchLineController {

    private final  WatchLineService watchLineService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param watchLine 巡更
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('hik_watchline_view')" )
    public R getWatchLinePage(Page page, WatchLine watchLine) {
        return R.ok(watchLineService.page(page, Wrappers.query(watchLine).lambda()
		.like(StringUtils.isNotEmpty(watchLine.getWatchLine()),WatchLine::getWatchLine,watchLine.getWatchLine())
		.between(StringUtils.isNotEmpty
				(watchLine.getStartTime())&&StringUtils.isNotEmpty
				(watchLine.getEndTime()),WatchLine::getCreateTime,watchLine.getStartTime(),watchLine.getEndTime())
		));
    }


    /**
     * 通过id查询巡更
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_watchline_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(watchLineService.getById(id));
    }

    /**
     * 新增巡更
     * @param watchLine 巡更
     * @return R
     */
    @ApiOperation(value = "新增巡更", notes = "新增巡更")
    @SysLog("新增巡更" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('hik_watchline_add')" )
    public R save(@RequestBody WatchLine watchLine) {
        return R.ok(watchLineService.save(watchLine));
    }

    /**
     * 修改巡更
     * @param watchLine 巡更
     * @return R
     */
    @ApiOperation(value = "修改巡更", notes = "修改巡更")
    @SysLog("修改巡更" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('hik_watchline_edit')" )
    public R updateById(@RequestBody WatchLine watchLine) {
        return R.ok(watchLineService.updateById(watchLine));
    }

    /**
     * 通过id删除巡更
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除巡更", notes = "通过id删除巡更")
    @SysLog("通过id删除巡更" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_watchline_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(watchLineService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param watchLine 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hik_watchline_export')" )
    public List<WatchLine> export(WatchLine watchLine) {
        return watchLineService.list(Wrappers.query(watchLine));
    }
}
